﻿@using Smartstore.Web.Rendering.Menus
@using Smartstore.Core.Content.Menus

@model MenuModel

@{
    var root = Model.Root;
    if (root == null)
    {
        return;
    }

    var hasIcons = root.Children.Any(x => x.Value.Icon.HasValue());
    var hasImages = root.Children.Any(x => x.Value.ImageUrl.HasValue());
}
        
<nav class="list-group list-group-flush has-icons cms-menu cms-menu-listgroup" role="menu" aria-orientation="vertical" data-menu-name="@(Model.Name?.ToLower())" aria-label="@Model.PublicName">
    @foreach (var node in Model.Root.Children.Where(x => x.Value.Visible))
    {
        var item = node.Value;
        var itemState = node.GetNodePathState(Model.Path);
        var itemText = node.GetItemText(T);
        var itemUrl = item.GenerateUrl(this.ViewContext);
        var isSelected = (itemState & NodePathState.Selected) == NodePathState.Selected;

        var attrs = item.GetCombinedAttributes();
        attrs.PrependCssClass("list-group-item list-group-item-action menu-link");
        if (isSelected)
        {
            attrs.AppendCssClass("active");
            attrs.Add("aria-current", "page");
            attrs.Add("tabindex", "-1");
        }

        <a href="@itemUrl" attrs="attrs" role="menuitem">
            @if (hasIcons)
            {
                <i class="fal fa-fw@(item.Icon.HasValue() ? item.Icon.EnsureStartsWith(" ") : " fas") list-group-item-icon" aria-hidden="true"></i>
            }
            else if (hasImages && item.ImageUrl.HasValue())
            {
                <img class="sm-icon" src="@Url.Content(item.ImageUrl)" alt="@itemText" aria-hidden="true" />
            }
            else if (hasImages)
            {
                <i class="fa-fw fas" aria-hidden="true"></i>
            }

            <span sm-language-attributes-for="item">@itemText</span>

            @if (item.BadgeText.HasValue())
            {
                <span class="badge badge-pill badge-counter badge-@(((BadgeStyle)item.BadgeStyle).ToString().ToLower()) ml-auto" attrs="item.BadgeHtmlAttributes">
                    @Html.Raw(item.BadgeText)
                </span>
            }
        </a>
    }
</nav>